<?php

/**
 * 菜单
 *
 * @author kj
 */

namespace Kj\Admin\Html;

use Kj\Admin\Libraries\Tree;
use Scstore, View;

class Menu
{
    /**
     * 菜单信息
     * 
     * @var array
     */
    protected $list;

    /**
     * 菜单信息
     * 
     * @var array
     */
    protected $menuTree;

    /**
     * construct
     *
     * @return  void
     */
    public function __construct()
    {
        $this->list = Scstore::getUserPermissionSession();
        foreach($this->list as $key => $value)
        {
            if($value['display'] == '0' && $value['id'] != HOME_PERMISSION_ID) unset($this->list[$key]);
        }
        $this->menuTree = (array) Tree::genTree($this->list);
    }

    /**
     * 顶部菜单
     *
     * @return void
     */
    public function topMenu()
    {
        $menuTree = $this->menuTree;
        return View::make('admin/widget/top_menu')->with(compact('menuTree'));
    }

    /**
     * 二级菜单
     * 
     * @return void
     */
    public function subMenu()
    {
        $sub = array();
        foreach($this->menuTree as $key => $value)
        {
            if(isset($value['son']) && CURRENT_PID == $value['id'])
            {
                foreach($value['son'] as $key2 => & $value2)
                {
                    if(isset($value2['son']))
                    {
                        $tmpVar = $value2['son']; unset($value2['son']);
                        $value2 = array_merge(array($value2), $tmpVar);
                    }
                    $sub[] = $value2;
                }
            }
        }
        return View::make('admin/widget/sub_menu')->with(compact('sub'));
    }

    /**
     * 二级菜单右边的功能按钮
     * 
     * @return void
     */
    public function subMenuAction()
    {
        $file = 'admin/widget/submr_'.CURRENT_PID;
        if(View::exists($file))
            return View::make($file);
        else
            return '';
    }
    
    /**
     * 底部面包屑
     *
     * @access public
     */
    public function footerCrumbs()
    {
        $list = Scstore::getUserPermissionSession();
        if( ! is_array($list)) return '';
        $son = \Kj\Admin\Libraries\Tree::getSonKey();
        $arr = array();
        foreach($this->menuTree as $pMenu)
        {
            if($pMenu['id'] == CURRENT_PID && isset($pMenu[$son]))
            {
                $arr['p'] = $pMenu;
                $currentSon = current($pMenu[$son]);
                $arr['p']['module'] = $currentSon['module'];
                $arr['p']['class'] = $currentSon['class'];
                $arr['p']['action'] = $currentSon['action'];
            }
        }
        foreach($list as $key => $value)
        {
            if($value['id'] == CURRENT_SID) $arr['s'] = $value;
            if($value['module'] == ROUTER_M && $value['class'] == ROUTER_C && $value['action'] == ROUTER_A)
            {
                $arr['t'] = $value;
            }
        }
        return View::make('admin/widget/footer_crumbs')->with(compact('arr'));
    }

}
